import json

from coin.exchange.base.kr_ws.ws_client_base import WebsocketUpdate
from coin.exchange.base.kr_ws.ws_parser_base import (WebsocketParserManagerBase, WsFeedHandlerBase)
import coin.proto.coin_market_enums_pb2 as coin_enums
import coin.proto.coin_feed_pb2 as coin_feed
from coin.proto.coin_feed_pb2 import (BookEntry,
                                      BookMessage,
                                      TradeMessage,
                                      FeedMessage,
                                      FeedMessageBundle)
from coin.proto.coin_market_query_pb2 import (ProductTicker, ExchangeTicker)


class CoinoneParser(object):
  @staticmethod
  def parse_ticker(update: WebsocketUpdate, json_msg: dict) -> ExchangeTicker:
    json_msg = json_msg['data']
    assert json_msg[0] == 'update'
    ticker_msg = json_msg[1]
    exchange_ticker = ExchangeTicker(exchange='Coinone')
    if ticker_msg:
      symbol_list = {key.split('_')[0] for key in ticker_msg}
    else:
      return exchange_ticker  # Contains no ticker.

    ticker_list = []
    for coin in symbol_list:
      ticker = ProductTicker()
      ticker.symbol = coin.upper()
      ticker.native_symbol = coin
      ticker.last = float(ticker_msg[coin + '_price'])
      ticker.volume_24hr = float(ticker_msg[coin + '_volume'])
      ticker.high = float(ticker_msg[coin + '_high'])
      ticker.low = float(ticker_msg[coin + '_low'])
      ticker_list.append(ticker)

    exchange_ticker.each_ticker.extend(ticker_list)
    return exchange_ticker

  @staticmethod
  def parse_book(update: WebsocketUpdate, orderbook_msg: dict, symbol: str) -> FeedMessage:
    ask_list = []
    bid_list = []
    for ask in json.loads(orderbook_msg['ASK']):
      book_entry = BookEntry()
      book_entry.price = int(ask['price'])
      book_entry.qty_float = float(ask['qty']) / 10000.0
      ask_list.append(book_entry)
    for bid in json.loads(orderbook_msg['BID']):
      book_entry = BookEntry()
      book_entry.price = int(bid['price'])
      book_entry.qty_float = float(bid['qty']) / 10000.0
      bid_list.append(book_entry)

    bid_list = sorted(bid_list, key=lambda elem: -elem.price)
    ask_list = sorted(ask_list, key=lambda elem: elem.price)
    book_message = BookMessage(ask=ask_list, bid=bid_list)
    return FeedMessage(
        feeder_id=0,
        machine_id=0,
        symbol=symbol,
        feed_type=coin_feed.BOOK_FEED,
        exchange_type=coin_enums.Coinone,
        book=book_message,
        fetched_time=int(update.timestamp * 10**9),
    )

  @staticmethod
  def parse_trade(update: WebsocketUpdate, json_msg: dict):
    symbol = json_msg['header']['currency']
    json_msg = json_msg['data']
    assert json_msg[0] == 'update'
    trade_msg = json_msg[1]
    trade_feed = TradeMessage(price=int(trade_msg['price']),
                              qty_float=float(trade_msg['volume']) / 10000.0)
    return FeedMessage(feeder_id=0,
                       machine_id=0,
                       symbol=symbol,
                       exchange_type=coin_enums.Coinone,
                       exchange_time=int(float(trade_msg['time_stamp']) * 10**9),
                       fetched_time=int(float(update.timestamp) * 10**9),
                       feed_type=coin_feed.TRADE_FEED,
                       trade=trade_feed)


class CoinoneWSParserManager(WebsocketParserManagerBase):
  def __init__(self, handler, symbol):
    super().__init__(handler=handler)
    self._symbol = symbol

  @staticmethod
  def _preparse(raw_feed):
    # 42/ticker,["update",{"btc_price":"12814000","btc_volume":"75864302","btc_high":"18200000","btc_low":"11510000","btc_yesterday_price":"15674000","eth_price":"1127000","eth_volume":"1617381841","eth_high":"1725900","eth_low":"986500","eth_yesterday_price":"1447300","etc_price":"31200","etc_volume":"11067987397","etc_high":"52880","etc_low":"26560","etc_yesterday_price":"43050","xrp_price":"1279","xrp_volume":"3041985408278","xrp_high":"2298","xrp_low":"1001","xrp_yesterday_price":"1802","bch_price":"1980500","bch_volume":"120195757","bch_high":"3219000","bch_low":"1796000","bch_yesterday_price":"2579500","qtum_price":"39300","qtum_volume":"19033998495","qtum_high":"68900","qtum_low":"31550","qtum_yesterday_price":"55430","ltc_price":"213000","ltc_volume":"350905178","ltc_high":"311450","ltc_low":"167700","ltc_yesterday_price":"265500","iota_price":"2900","iota_volume":"116042162523","iota_high":"4740","iota_low":"2580","iota_yesterday_price":"3990","btg_price":"433000","btg_volume":"128442783","btg_high":"740050","btg_low":"389950","btg_yesterday_price":"566400"}]
    # 42/trade_btc,["update",{"time_stamp":"1516160082","price":"12707000","volume":"155"}]
    # 42/orderbook,["update",{"BID":"[{\"price\":\"12814000\",\"qty\":\"103\"},{\"price\":\"12810000\",\"qty\":\"18000\"},{\"price\":\"12632000\",\"qty\":\"5557\"},{\"price\":\"12631000\",\"qty\":\"9440\"},{\"price\":\"12556000\",\"qty\":\"1000\"},{\"price\":\"12500000\",\"qty\":\"105362\"},{\"price\":\"12496000\",\"qty\":\"1\"},{\"price\":\"12491000\",\"qty\":\"200\"},{\"price\":\"12480000\",\"qty\":\"1\"},{\"price\":\"12475000\",\"qty\":\"200\"},{\"price\":\"12464000\",\"qty\":\"1\"},{\"price\":\"12449000\",\"qty\":\"94320\"},{\"price\":\"12448000\",\"qty\":\"1\"},{\"price\":\"12432000\",\"qty\":\"1\"},{\"price\":\"12421000\",\"qty\":\"200\"},{\"price\":\"12416000\",\"qty\":\"1\"},{\"price\":\"12400000\",\"qty\":\"100001\"},{\"price\":\"12384000\",\"qty\":\"1\"},{\"price\":\"12383000\",\"qty\":\"200\"},{\"price\":\"12380000\",\"qty\":\"3000\"},{\"price\":\"12376000\",\"qty\":\"2000\"},{\"price\":\"12370000\",\"qty\":\"7853\"},{\"price\":\"12368000\",\"qty\":\"1\"},{\"price\":\"12361000\",\"qty\":\"30\"},{\"price\":\"12352000\",\"qty\":\"1001\"},{\"price\":\"12350000\",\"qty\":\"500\"},{\"price\":\"12347000\",\"qty\":\"30959\"},{\"price\":\"12336000\",\"qty\":\"1\"},{\"price\":\"12324000\",\"qty\":\"200\"},{\"price\":\"12320000\",\"qty\":\"1\"},{\"price\":\"12300000\",\"qty\":\"1942\"},{\"price\":\"12273000\",\"qty\":\"2000\"},{\"price\":\"12272000\",\"qty\":\"80\"},{\"price\":\"12262000\",\"qty\":\"1067\"},{\"price\":\"12250000\",\"qty\":\"650\"},{\"price\":\"12210000\",\"qty\":\"500\"},{\"price\":\"12200000\",\"qty\":\"40445\"},{\"price\":\"12199000\",\"qty\":\"6782\"},{\"price\":\"12190000\",\"qty\":\"500\"},{\"price\":\"12180000\",\"qty\":\"201\"},{\"price\":\"12170000\",\"qty\":\"200\"},{\"price\":\"12150000\",\"qty\":\"6055\"},{\"price\":\"12144000\",\"qty\":\"200\"},{\"price\":\"12110000\",\"qty\":\"10000\"},{\"price\":\"12104000\",\"qty\":\"200\"},{\"price\":\"12100000\",\"qty\":\"3428\"},{\"price\":\"12095000\",\"qty\":\"322\"},{\"price\":\"12061000\",\"qty\":\"30\"},{\"price\":\"12052000\",\"qty\":\"1000\"},{\"price\":\"12050000\",\"qty\":\"50\"},{\"price\":\"12046000\",\"qty\":\"94320\"},{\"price\":\"12044000\",\"qty\":\"1077\"},{\"price\":\"12010000\",\"qty\":\"6368\"},{\"price\":\"12005000\",\"qty\":\"5000\"},{\"price\":\"12001000\",\"qty\":\"12058\"},{\"price\":\"12000000\",\"qty\":\"87902\"},{\"price\":\"11997000\",\"qty\":\"900\"},{\"price\":\"11990000\",\"qty\":\"1000\"},{\"price\":\"11987000\",\"qty\":\"100\"},{\"price\":\"11980000\",\"qty\":\"83\"},{\"price\":\"11979000\",\"qty\":\"11\"},{\"price\":\"11950000\",\"qty\":\"50\"},{\"price\":\"11931000\",\"qty\":\"3821\"},{\"price\":\"11930000\",\"qty\":\"1370\"},{\"price\":\"11920000\",\"qty\":\"200\"},{\"price\":\"11910000\",\"qty\":\"200\"},{\"price\":\"11900000\",\"qty\":\"50\"},{\"price\":\"11887000\",\"qty\":\"100\"},{\"price\":\"11864000\",\"qty\":\"200\"},{\"price\":\"11822000\",\"qty\":\"10000\"},{\"price\":\"11810000\",\"qty\":\"100\"},{\"price\":\"11806000\",\"qty\":\"300\"},{\"price\":\"11800000\",\"qty\":\"7054\"},{\"price\":\"11784000\",\"qty\":\"200\"},{\"price\":\"11780000\",\"qty\":\"500\"},{\"price\":\"11778000\",\"qty\":\"500\"},{\"price\":\"11770000\",\"qty\":\"100\"},{\"price\":\"11761000\",\"qty\":\"30\"},{\"price\":\"11756000\",\"qty\":\"479\"},{\"price\":\"11750000\",\"qty\":\"1250\"},{\"price\":\"11700000\",\"qty\":\"6972\"},{\"price\":\"11670000\",\"qty\":\"200\"},{\"price\":\"11650000\",\"qty\":\"1500\"},{\"price\":\"11640000\",\"qty\":\"767\"},{\"price\":\"11615000\",\"qty\":\"3558\"},{\"price\":\"11610000\",\"qty\":\"11348\"},{\"price\":\"11601000\",\"qty\":\"3733\"},{\"price\":\"11600000\",\"qty\":\"33016\"},{\"price\":\"11594000\",\"qty\":\"1000\"},{\"price\":\"11590000\",\"qty\":\"100\"},{\"price\":\"11587000\",\"qty\":\"200\"},{\"price\":\"11552000\",\"qty\":\"1000\"},{\"price\":\"11550000\",\"qty\":\"14720\"},{\"price\":\"11540000\",\"qty\":\"200\"},{\"price\":\"11533000\",\"qty\":\"100\"},{\"price\":\"11520000\",\"qty\":\"3111\"},{\"price\":\"11516000\",\"qty\":\"503\"},{\"price\":\"11511000\",\"qty\":\"700\"},{\"price\":\"11510000\",\"qty\":\"5478\"},{\"price\":\"11505000\",\"qty\":\"10000\"},{\"price\":\"11500000\",\"qty\":\"138627\"},{\"price\":\"11480000\",\"qty\":\"257\"},{\"price\":\"11479000\",\"qty\":\"2345\"},{\"price\":\"11464000\",\"qty\":\"1\"},{\"price\":\"11461000\",\"qty\":\"30\"},{\"price\":\"11460000\",\"qty\":\"200\"},{\"price\":\"11449000\",\"qty\":\"33\"},{\"price\":\"11420000\",\"qty\":\"200\"},{\"price\":\"11405000\",\"qty\":\"117\"},{\"price\":\"11400000\",\"qty\":\"1244\"},{\"price\":\"11394000\",\"qty\":\"1\"},{\"price\":\"11377000\",\"qty\":\"100\"},{\"price\":\"11350000\",\"qty\":\"600\"},{\"price\":\"11347000\",\"qty\":\"100\"},{\"price\":\"11340000\",\"qty\":\"9010\"},{\"price\":\"11332000\",\"qty\":\"2000\"},{\"price\":\"11324000\",\"qty\":\"1\"},{\"price\":\"11320000\",\"qty\":\"200\"},{\"price\":\"11310000\",\"qty\":\"100\"},{\"price\":\"11305000\",\"qty\":\"30000\"},{\"price\":\"11300000\",\"qty\":\"11030\"},{\"price\":\"11291000\",\"qty\":\"1000\"},{\"price\":\"11287000\",\"qty\":\"100\"},{\"price\":\"11272000\",\"qty\":\"100\"},{\"price\":\"11260000\",\"qty\":\"10\"},{\"price\":\"11255000\",\"qty\":\"10000\"},{\"price\":\"11254000\",\"qty\":\"1\"},{\"price\":\"11250000\",\"qty\":\"1250\"},{\"price\":\"11230000\",\"qty\":\"400\"},{\"price\":\"11200000\",\"qty\":\"41740\"},{\"price\":\"11194000\",\"qty\":\"1\"},{\"price\":\"11190000\",\"qty\":\"45351\"},{\"price\":\"11180000\",\"qty\":\"1120\"},{\"price\":\"11177000\",\"qty\":\"100\"},{\"price\":\"11170000\",\"qty\":\"1770\"},{\"price\":\"11150000\",\"qty\":\"1025\"},{\"price\":\"11135000\",\"qty\":\"500\"},{\"price\":\"11132000\",\"qty\":\"600\"},{\"price\":\"11124000\",\"qty\":\"1\"},{\"price\":\"11111000\",\"qty\":\"10000\"},{\"price\":\"11105000\",\"qty\":\"10000\"},{\"price\":\"11100000\",\"qty\":\"5038\"},{\"price\":\"11097000\",\"qty\":\"1000\"},{\"price\":\"11070000\",\"qty\":\"10000\"},{\"price\":\"11064000\",\"qty\":\"1\"},{\"price\":\"11062000\",\"qty\":\"200\"},{\"price\":\"11047000\",\"qty\":\"3000\"},{\"price\":\"11020000\",\"qty\":\"6066\"},{\"price\":\"11010000\",\"qty\":\"2184\"},{\"price\":\"11001000\",\"qty\":\"520\"},{\"price\":\"11000000\",\"qty\":\"102029\"}]","ASK":"[{\"price\":\"12832000\",\"qty\":\"8782\"},{\"price\":\"12837000\",\"qty\":\"1212\"},{\"price\":\"12849000\",\"qty\":\"10\"},{\"price\":\"12852000\",\"qty\":\"11\"},{\"price\":\"12881000\",\"qty\":\"5419\"},{\"price\":\"12882000\",\"qty\":\"20000\"},{\"price\":\"12909000\",\"qty\":\"72185\"},{\"price\":\"12943000\",\"qty\":\"7464\"},{\"price\":\"12950000\",\"qty\":\"688\"},{\"price\":\"13000000\",\"qty\":\"63961\"},{\"price\":\"13084000\",\"qty\":\"183\"},{\"price\":\"13122000\",\"qty\":\"466\"},{\"price\":\"13152000\",\"qty\":\"1\"},{\"price\":\"13168000\",\"qty\":\"1\"},{\"price\":\"13184000\",\"qty\":\"1\"},{\"price\":\"13200000\",\"qty\":\"142\"},{\"price\":\"13216000\",\"qty\":\"1\"},{\"price\":\"13232000\",\"qty\":\"1\"},{\"price\":\"13248000\",\"qty\":\"1\"},{\"price\":\"13264000\",\"qty\":\"1\"},{\"price\":\"13280000\",\"qty\":\"1\"},{\"price\":\"13296000\",\"qty\":\"1\"},{\"price\":\"13300000\",\"qty\":\"4643\"},{\"price\":\"13312000\",\"qty\":\"1\"},{\"price\":\"13325000\",\"qty\":\"9430\"},{\"price\":\"13328000\",\"qty\":\"1\"},{\"price\":\"13337000\",\"qty\":\"170\"},{\"price\":\"13341000\",\"qty\":\"2\"},{\"price\":\"13344000\",\"qty\":\"1\"},{\"price\":\"13360000\",\"qty\":\"1\"},{\"price\":\"13376000\",\"qty\":\"1\"},{\"price\":\"13392000\",\"qty\":\"1\"},{\"price\":\"13408000\",\"qty\":\"1\"},{\"price\":\"13424000\",\"qty\":\"1\"},{\"price\":\"13440000\",\"qty\":\"1\"},{\"price\":\"13450000\",\"qty\":\"1592\"},{\"price\":\"13456000\",\"qty\":\"1\"},{\"price\":\"13472000\",\"qty\":\"1\"},{\"price\":\"13488000\",\"qty\":\"1\"},{\"price\":\"13500000\",\"qty\":\"8810\"},{\"price\":\"13504000\",\"qty\":\"1\"},{\"price\":\"13510000\",\"qty\":\"200\"},{\"price\":\"13520000\",\"qty\":\"1\"},{\"price\":\"13536000\",\"qty\":\"1\"},{\"price\":\"13552000\",\"qty\":\"1\"},{\"price\":\"13568000\",\"qty\":\"1\"},{\"price\":\"13584000\",\"qty\":\"1\"},{\"price\":\"13600000\",\"qty\":\"3\"},{\"price\":\"13602000\",\"qty\":\"12180\"},{\"price\":\"13615000\",\"qty\":\"600\"},{\"price\":\"13616000\",\"qty\":\"1\"},{\"price\":\"13632000\",\"qty\":\"1\"},{\"price\":\"13648000\",\"qty\":\"1\"},{\"price\":\"13664000\",\"qty\":\"1\"},{\"price\":\"13680000\",\"qty\":\"1\"},{\"price\":\"13696000\",\"qty\":\"1\"},{\"price\":\"13703000\",\"qty\":\"74\"},{\"price\":\"13712000\",\"qty\":\"1\"},{\"price\":\"13728000\",\"qty\":\"1\"},{\"price\":\"13744000\",\"qty\":\"1\"},{\"price\":\"13755000\",\"qty\":\"2000\"},{\"price\":\"13760000\",\"qty\":\"1\"},{\"price\":\"13776000\",\"qty\":\"1\"},{\"price\":\"13790000\",\"qty\":\"1000\"},{\"price\":\"13792000\",\"qty\":\"1\"},{\"price\":\"13800000\",\"qty\":\"500\"},{\"price\":\"13808000\",\"qty\":\"1\"},{\"price\":\"13820000\",\"qty\":\"4997\"},{\"price\":\"13824000\",\"qty\":\"1\"},{\"price\":\"13840000\",\"qty\":\"1\"},{\"price\":\"13856000\",\"qty\":\"1\"},{\"price\":\"13860000\",\"qty\":\"280\"},{\"price\":\"13867000\",\"qty\":\"2997\"},{\"price\":\"13872000\",\"qty\":\"1\"},{\"price\":\"13888000\",\"qty\":\"1\"},{\"price\":\"13900000\",\"qty\":\"1057\"},{\"price\":\"13904000\",\"qty\":\"1\"},{\"price\":\"13920000\",\"qty\":\"1\"},{\"price\":\"13936000\",\"qty\":\"1\"},{\"price\":\"13940000\",\"qty\":\"32\"},{\"price\":\"13952000\",\"qty\":\"1\"},{\"price\":\"13953000\",\"qty\":\"41\"},{\"price\":\"13960000\",\"qty\":\"105\"},{\"price\":\"13968000\",\"qty\":\"1\"},{\"price\":\"13984000\",\"qty\":\"1\"},{\"price\":\"13993000\",\"qty\":\"100\"},{\"price\":\"13994000\",\"qty\":\"100\"},{\"price\":\"13997000\",\"qty\":\"100\"},{\"price\":\"14000000\",\"qty\":\"20100\"},{\"price\":\"14016000\",\"qty\":\"1\"},{\"price\":\"14032000\",\"qty\":\"1\"},{\"price\":\"14040000\",\"qty\":\"100\"},{\"price\":\"14041000\",\"qty\":\"22500\"},{\"price\":\"14044000\",\"qty\":\"200\"},{\"price\":\"14045000\",\"qty\":\"120\"},{\"price\":\"14048000\",\"qty\":\"1\"},{\"price\":\"14050000\",\"qty\":\"761\"},{\"price\":\"14064000\",\"qty\":\"1\"},{\"price\":\"14070000\",\"qty\":\"100\"},{\"price\":\"14080000\",\"qty\":\"1\"},{\"price\":\"14089000\",\"qty\":\"100\"},{\"price\":\"14094000\",\"qty\":\"3096\"},{\"price\":\"14096000\",\"qty\":\"1\"},{\"price\":\"14109000\",\"qty\":\"660\"},{\"price\":\"14112000\",\"qty\":\"1\"},{\"price\":\"14122000\",\"qty\":\"67\"},{\"price\":\"14128000\",\"qty\":\"1\"},{\"price\":\"14129000\",\"qty\":\"100\"},{\"price\":\"14130000\",\"qty\":\"100\"},{\"price\":\"14140000\",\"qty\":\"3994\"},{\"price\":\"14144000\",\"qty\":\"1\"},{\"price\":\"14150000\",\"qty\":\"362\"},{\"price\":\"14160000\",\"qty\":\"1\"},{\"price\":\"14176000\",\"qty\":\"1\"},{\"price\":\"14192000\",\"qty\":\"1\"},{\"price\":\"14200000\",\"qty\":\"999\"},{\"price\":\"14208000\",\"qty\":\"1\"},{\"price\":\"14224000\",\"qty\":\"1\"},{\"price\":\"14236000\",\"qty\":\"395\"},{\"price\":\"14240000\",\"qty\":\"1\"},{\"price\":\"14256000\",\"qty\":\"1\"},{\"price\":\"14270000\",\"qty\":\"693\"},{\"price\":\"14272000\",\"qty\":\"1\"},{\"price\":\"14288000\",\"qty\":\"1\"},{\"price\":\"14300000\",\"qty\":\"10031\"},{\"price\":\"14304000\",\"qty\":\"1\"},{\"price\":\"14320000\",\"qty\":\"1\"},{\"price\":\"14336000\",\"qty\":\"1\"},{\"price\":\"14352000\",\"qty\":\"1\"},{\"price\":\"14368000\",\"qty\":\"1\"},{\"price\":\"14384000\",\"qty\":\"1\"},{\"price\":\"14400000\",\"qty\":\"505\"},{\"price\":\"14416000\",\"qty\":\"1\"},{\"price\":\"14432000\",\"qty\":\"1\"},{\"price\":\"14448000\",\"qty\":\"1\"},{\"price\":\"14464000\",\"qty\":\"1\"},{\"price\":\"14480000\",\"qty\":\"1\"},{\"price\":\"14486000\",\"qty\":\"200\"},{\"price\":\"14487000\",\"qty\":\"300\"},{\"price\":\"14489000\",\"qty\":\"19\"},{\"price\":\"14490000\",\"qty\":\"5296\"},{\"price\":\"14496000\",\"qty\":\"1\"},{\"price\":\"14500000\",\"qty\":\"22811\"},{\"price\":\"14501000\",\"qty\":\"100\"},{\"price\":\"14512000\",\"qty\":\"1\"},{\"price\":\"14514000\",\"qty\":\"333\"},{\"price\":\"14515000\",\"qty\":\"100\"},{\"price\":\"14516000\",\"qty\":\"100\"},{\"price\":\"14528000\",\"qty\":\"1\"},{\"price\":\"14537000\",\"qty\":\"220\"},{\"price\":\"14544000\",\"qty\":\"1\"}]","DIFF":{"BID":[{"price":"12814000","qty":"176"}],"ASK":[]}}]
    sepidx = raw_feed.index(',')
    json_msg = json.loads(raw_feed[sepidx + 1:])
    header_str = raw_feed[:sepidx]
    header_code, header_type = header_str.split('/')
    if header_type.startswith('trade'):
      header_type, currency = header_type.split('_')
      header = dict(code=header_code, type=header_type, currency=currency.upper())
    else:
      header = dict(code=header_code, type=header_type)
    return dict(header=header, data=json_msg)

  def on_feed(self, update, msg):
    if msg['header']['type'] == 'trade':
      assert msg['header']['currency'] == self._symbol
      pb = CoinoneParser.parse_trade(update, msg)
      self._handler.on_trade(pb)
    elif msg['header']['type'] == 'orderbook':
      key = msg['data'][0]
      value = msg['data'][1]
      if key == 'update' and value is not None:
        pb = CoinoneParser.parse_book(update, value, self._symbol)
        self._handler.on_book(pb)
    elif msg['header']['type'] == 'ticker':  # Not interested.
      # pb = CoinoneParser.parse_ticker(update, msg)
      # self._handler.on_ticker(pb)
      pass
    else:  # Not interested.
      pass

  @staticmethod
  def parse_message(msg):
    if msg == '3':
      # Heartbeat request message '2', heartbeat responce message '3'.
      # Need to send periodically, every 25 seconds.
      return None
    elif msg.startswith('40'):
      # Request echo
      return None
    elif msg.startswith('0{'):
      # First message received after connection is made
      return None
    elif msg.startswith('42/') or msg.startswith('44/'):
      return CoinoneWSParserManager._preparse(msg)
    elif msg == "" or msg == " ":
      return None
    else:
      raise ValueError('Unexpected message: %s' % msg)

  def on_recv_msg(self, update):
    msg = self.parse_message(update.msg)
    if msg is not None:
      self.on_feed(update, msg)
